﻿using Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 

namespace HookupAppForm
{
    public partial class FormMap : Form
    {
        public FormMap()
        {
            InitializeComponent();
        }
        private Image currentImg { get; set; }
        private int zoomStep = 100;
        private string url { get; set; } = Application.StartupPath + @"\svg\method-draw-image.svg";
        private void FormMap_Load(object sender, EventArgs e)
        {
            url = Utils.GetFileUrl();
            if (url!=null)
            {
                pictureBox1.AllowDrop = true;
                GetImg();
                pictureBox1.Location = new Point(0, 0);
                pictureBox1.MouseDown += (senders, e) =>
                {
                    string[] files = new string[1];
                    files[0] = url;
                    DoDragDrop(new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy | DragDropEffects.Move);
                };
            }
            else
            {
                Utils.SysMsg("请选择文件", MessageBoxIcon.Warning);
            }
        }
        private void GetImg(int width = 0, int height = 0)
        {
            currentImg = Utils.GetImageFromSVG(url, width, height);
            pictureBox1.Width = currentImg.Width;
            pictureBox1.Height = currentImg.Height;
            pictureBox1.Image = currentImg;
        }
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                pictureBox1.Width += zoomStep;
                pictureBox1.Height += zoomStep;
            }
            if (e.Delta < 0)
            {

                if (pictureBox1.Width < currentImg.Width / 10)
                    return;

                pictureBox1.Width -= zoomStep;
                pictureBox1.Height -= zoomStep;
            }
            GetImg(pictureBox1.Width, pictureBox1.Height);
        }      

        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        public static extern void ReleaseCapture();
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern void SendMessage(int hwnd, int wMsg, int wParam, int lParam);
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage((int)pictureBox1.Handle, 0xA1, 2, 0);
            }
        }
    }
}
